package com.linln.wtp.controller.roadrescue;

import com.linln.common.utils.DateUtil;
import com.linln.common.utils.SessionUtil;
import com.linln.common.vo.JsonResult;
import com.linln.component.wtpshiro.annotation.UserLoginToken;
import com.linln.modules.receiverecord.domain.ReceiveRecord;
import com.linln.modules.roadrescue.domain.RoadRescue;
import com.linln.modules.roadrescue.service.RoadRescueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.wtp.controller.roadrescue
 * @Description: 道路救援
 * @date Date : 2021年01月28日 11:34
 */
@RestController
@RequestMapping("roadRescue")
public class RoadRescueController {

    @Autowired
    private RoadRescueService roadRescueService;

    @PostMapping
    @UserLoginToken
    public JsonResult addRoadRescue(RoadRescue roadRescue) {
        roadRescue.setUserId(SessionUtil.getUserId());
        roadRescue.setAlarmDate(DateUtil.formatDate(new Date()));
        return roadRescueService.addRoadRescue(roadRescue) ? JsonResult.ok() : JsonResult.error();
    }

    @GetMapping
//    @UserLoginToken
    public JsonResult getRoadRescueList(@RequestParam(required = false, defaultValue = "0", value = "pageNum") Integer pageNum, @RequestParam(required = false, defaultValue = "10") Integer limit) {
        try {
            return roadRescueService.getRoadRescueList(pageNum+1,limit,SessionUtil.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("获取失败");
        }
    }

    @GetMapping("getRoadRescue")
    public JsonResult getRoadRescue(String id){
        RoadRescue roadRescue = roadRescueService.getById(id);
        List<String> mapList = new ArrayList<>();
        //发起时间
        mapList.add(roadRescue.getAlarmDate());
        Map<String,Object> props = roadRescue.getProps();
        if(props!=null){
            if(props.containsKey("processing")){
                Map m = (Map) props.get("processing");
                //处理时间
                mapList.add(m.get("handlerDate").toString());
            }
            if(props.containsKey("processed")){
                Map m = (Map) props.get("processed");
                //完成时间
                mapList.add(m.get("handlerDate").toString());
            }
        }

        return JsonResult.ok().put("data",roadRescue).put("timeList",mapList);
    }
}
